<template>
<view class="container">
 <!--  <view class='addrtop' style='background: {{setting.platformSetting.defaultColor}};'><view></view></view>
   -->
  <view class="successIcon">
      <icon type="success" size="40" :color="setting.platformSetting.defaultColor"></icon>
      <text class="success_text" v-if="success.payType == 0">已提交成功，请稍后~</text>
  </view>

  <view class="Items"><text>订单编号:</text>  <text>{{success.orderNo}}</text></view>  <text></text>
  <view class="Items"><text>订单应付金额: </text> <text>￥ {{success.goodsAmount}}</text> </view>
  <view class="Items" v-if="success.prepayAmount>0"><text>订单预付金额: </text> <text>￥{{success.prepayAmount}}</text> </view>
  <view class="Items" v-if="success.needJifen>0"><text>订单所需积分: </text> <text>{{success.needJifen}}</text> </view>
  <view class="Items"> <text>优惠金额: </text> <text>￥ {{success.youhuiAmount}}</text> </view>
  <view class="Items"><text>支付方式: </text> <text>{{success.payTypeStr}}</text> </view>
  <!-- <block wx:if='{{success.payType == 2}}'>
    <view class='Items'>充值说明: <text class='main' style="color:{{setting.platformSetting.defaultColor}}"
     bindtap='toAccount'>点击查看余额充值方式</text></view>
  </block> -->



  <button class="butn" v-if="success.payType != 0" :loading="butn_show_loading" :data-orderNo="success.orderNo" @tap="payNow" :style="'background:' + setting.platformSetting.defaultColor">立即支付</button>
  <button class="butn" @tap="toIndex" :style="'background:' + setting.platformSetting.defaultColor">返回首页</button>
</view>
</template>

<script>

export default {
  data() {
    return {
      success: null,
      setting: null,
      butn_show_loading: false,
      loginUser: "",
      properties: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(getApp().globalData.payItem);
    this.setData({
      success: getApp().globalData.payItem
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting,
      loginUser: getApp().globalData.loginUser,
      properties: getApp().globalData.properties
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    /* 去充值 */
    toAccount: function () {
      wx.navigateTo({
        url: '/pages/user_recharge/index'
      });
    },

    /* 立即支付 */
    payNow: function (e) {
      var orderNo = e.currentTarget.dataset.orderno;
      var orderItem = this.success;

      if (orderItem.payType == 2) {
        console.log('-----余额支付-----');
        this.payByYue(orderItem.orderNo);
      }

      if (orderItem.payType == 3) {
        console.log('-----微信支付-----');
        this.payByWechat(orderItem.orderNo);
      }
    },
    //微信支付
    payByWechat: function (orderNo) {
      var that = this;
      let loginUser = getApp().globalData.loginUser;
      console.log(loginUser);
      let wxChatPayParam = {
        openid: '',
        orderNo: '',
        app: 3
      };
      this.setData({
        butn_show_loading: true
      });
      wxChatPayParam.openid = loginUser.platformUser.miniOpenId;
      wxChatPayParam.orderNo = orderNo;
      console.log(wxChatPayParam);
      let customIndex = getApp().globalData.AddClientUrl("/unifined_order.html", wxChatPayParam, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          let PayStr = res.data;
          PayStr = '{' + PayStr + '}';
          let wechatPayStr = JSON.parse(PayStr);
          console.log(wechatPayStr);
          wx.requestPayment({
            'timeStamp': wechatPayStr.timeStamp,
            'nonceStr': wechatPayStr.nonceStr,
            'package': wechatPayStr.package,
            'signType': wechatPayStr.signType,
            'paySign': wechatPayStr.paySign,
            'success': function (res) {
              console.log('------成功--------');
              console.log(res);
              console.log(that.success);
              /* wx.showToast({
                title: '付款成功',
                icon: 'success',
                duration: 2000
              }) */

              if (that.success.orderType == 12) {
                wx.redirectTo({
                  url: '/pages/order_pintuan_detail/index?orderNo=' + that.success.orderNo
                });
              } else {
                if (that.success.mendianZiti == 1) {
                  if(that.success.payStatus!=3){
                      setTimeout(function () {
                        wx.redirectTo({
                          url: '/pages/order_detail/index?orderNo=' + orderNo
                        });
                      }, 2000);
                  }else{
                      setTimeout(function () {
                        wx.redirectTo({
                          url: '/pages/order_list_tab/index'
                        });
                      }, 2000);
                      // setTimeout(function () {
                      //   getApp().globalData.linkEvent('my_ziti_orders.html','reLaunch');
                      // }, 2000);
                  }
                } else {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/order_list_tab/index'
                    });
                  }, 2000);
                }
              }
              /* setTimeout(function () {
                
              }, 2000) */

            },
            'fail': function (res) {
              console.log('------fail--------');
              console.log(res);
            },
            'complete': function () {
              console.log('------complete--------');
              console.log(res);
              that.setData({
                butn_show_loading: false
              });
            }
          });
        },
        complete: function () {}
      });
    },

    tolinkUrl: function (e) {
      console.log("tolinkUrl", e);
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      getApp().globalData.linkEvent(linkUrl);
    },
    /* 余额支付 */
    payByYue: function (orderNo) {
      var that = this;
      console.log(orderNo);
      wx.showModal({
        title: '提示',
        content: '确认付款',
        success: function (res) {
          if (res.confirm) {
            let param_post = {};
            param_post.orderNo = orderNo;
            var customIndex = getApp().globalData.AddClientUrl("/order_account_pay.html", param_post, 'post');
            that.setData({
              butn_show_loading: true
            });
            wx.request({
              url: customIndex.url,
              data: customIndex.params,
              header: getApp().globalData.headerPost,
              method: 'POST',
              success: function (res) {
                console.log(res.data);

                if (res.data.errcode == '0') {
                  wx.showToast({
                    title: '付款成功',
                    icon: 'success',
                    duration: 2000
                  });

                  if (res.data.relateObj.mendianZiti == 1) {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/order_detail/index?orderNo=' + orderNo
                      });
                    }, 2000);
                  } else {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/order_list_tab/index'
                      });
                    }, 2000);
                  }
                } else {
                  wx.showToast({
                    title: res.data.errMsg,
                    image: '/wxcomponents/images/icons/tip.png',
                    duration: 2000
                  });
                }
              },
              fail: function (res) {
                getApp().globalData.loadFail();
              },
              complete: function (res) {
                that.setData({
                  butn_show_loading: false
                });
              }
            });
          } else if (res.cancel) {}
        },
        complete: function () {}
      });
    },
    toIndex: function () {
      // console.log('首页叫做：' + app.miniIndexPage)
      // console.log('首页叫做：' + app.clientNo)
      // //这个需要注意  switchTab  和  redirectTo
      // if (app.clientNo == 'tunzai') {
      //   console.log("1111111111111")
      //   wx.switchTab({
      //     url: '/pageTab/lanHu/index/index',
      //   })
      //   return;
      // }
      // else if (app.miniIndexPage) {
      //   console.log("2222222222222")
      //   wx.switchTab({
      //     url: '/pageTab/' + app.miniIndexPage + '/index',
      //   })
      // } else {
      //   console.log("33333333333333")
      //   wx.switchTab({
      //     url: '/pageTab/custom_page_index/index',
      //   })
      // }
      getApp().globalData.toIndex();
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>